package com.ruoyi.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.wechat.common.tree.TreeFactory;
import com.ruoyi.wechat.domain.AddrItem;
import com.ruoyi.wechat.domain.vo.AjaxResult;
import com.ruoyi.wechat.service.IAddrItemService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址选项
 *
 * @author YinHeng
 * @date 2024/2/21 9:11
 */
@RestController
@RequestMapping("/addrItem")
public class AddrItemController {

    @Resource
    private IAddrItemService addrItemService;

    /**
     * 用户地址级联选择
     *
     * @return
     */
    @GetMapping("/options")
    public AjaxResult options(AddrItem form) {
        LambdaQueryWrapper<AddrItem> query = new LambdaQueryWrapper<>();
        query.eq(AddrItem::getArea, form.getArea());
        List<AddrItem> list = addrItemService.list(query);
        TreeFactory<Long, AddrItem> treeFactory = new TreeFactory<>();
        List<AddrItem> addrTree = treeFactory.buildTree(list);
        return AjaxResult.success(addrTree);
    }


}
